package cn.tedu.csmall.resource.controller;

import cn.tedu.csmall.commons.exception.ServiceException;
import cn.tedu.csmall.commons.response.ServiceCode;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

import static cn.tedu.csmall.commons.response.ServiceCode.NOT_ACCEPTABLE;

@RestController
@RequestMapping("/upload")
public class FileUploadController {

    @PostMapping("/avatar")
    public String uploadAvatar(MultipartFile file) throws Throwable {
        String targetParentDirName = "avatar";
        String targetSubDirName = generateDirNameByDate();
        upload(file, targetParentDirName, targetSubDirName);
        return "OK";
    }

    @PostMapping("/brand-icon")
    public String uploadBrandIcon(MultipartFile file, String brandPinyin) throws Throwable {
        String targetParentDirName = "brand-icon";
        upload(file, targetParentDirName, brandPinyin);
        return "OK";
    }

    private void upload(MultipartFile file, String targetParentDirName, String targetSubDirName) throws Throwable {
        //判断是否存在上传数据
        if(file == null || file.isEmpty()){
            throw new ServiceException(NOT_ACCEPTABLE,"");
        }
        //判断文件类型, 例如: image/jpeg
        String contentType = file.getContentType();

        // 上传的文件夹
        String baseDir = "E:/";
        File targetParentDir = new File(baseDir, targetParentDirName);
        File targetSubDir = new File(targetParentDir, targetSubDirName);
        if (!targetSubDir.exists()) {
            targetSubDir.mkdirs();
        }

        // 上传的文件名
        String fileName = generateFileNameByTime()
                + "-" + System.nanoTime()
                + "-" + getRandomValue();

        // 上传的扩展名
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i);

        // 上传的文件的全名
        String fileFullName = fileName + suffix;

        File dest = new File(targetSubDir, fileFullName);
        file.transferTo(dest);
    }

    /**
     *
     */
    private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    /**
     *
     */
    private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    /**
     * 根据日期生成文件夹名称
     * @return 当前日期对应的文件夹名称
     */
    private String generateDirNameByDate() {
        LocalDateTime now = LocalDateTime.now();
        return dateFormatter.format(now);
    }

    private String generateFileNameByTime() {
        LocalDateTime now = LocalDateTime.now();
        return dateTimeFormatter.format(now);
    }

    private Random random = new Random();

    private int getRandomValue() {
        return random.nextInt(900000) + 100000;
    }

}
